﻿using Kingdee.BOS.Orm.DataEntity;
using SD.WRZS.K3.ServiceFacade.KDServiceEntity.Mobile;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SDWXServer.Models
{
    /// <summary>
    /// 销售订单
    /// </summary>
    [Serializable]
    public class WXDeliveryNotice : AbstractBillObject
    {
        public WXDeliveryNotice()
        {
            Organization = new Organization();
            Material = new Material();
            DocumentStatusName = "";
            DeliveryDate = DateTime.Now.Date;
            Customer = new Customer();
            Receiver = new Customer();
            Carrier = new Supplier();
            PCJH = new WXPCJHItem();
        }
        /// <summary>
        /// 分录内码
        /// </summary>
        public Int64 EntryId { get; set; }

        /// <summary>
        /// 单据状态名称
        /// </summary>
        public string DocumentStatusName { get; set; }
        /// <summary>
        /// 备注
        /// </summary>
        public string Note { get; set; }
        /// <summary>
        /// 提货地点
        /// </summary>
        public Organization Organization { get; set; }
        /// <summary>
        /// 物料
        /// </summary>
        public Material Material { get; set; }
        /// <summary>
        /// 计划数量
        /// </summary>
        public decimal PlanQty { get; set; }
        /// <summary>
        /// 出库数量
        /// </summary>
        public decimal StockQty { get; set; }

        /// <summary>
        /// 交货日期
        /// </summary>
        public DateTime DeliveryDate { get; set; }
        /// <summary>
        /// 客户
        /// </summary>
        public Customer Customer { get; set; }
        /// <summary>
        /// 收货方
        /// </summary>
        public Customer Receiver { get; set; }
        /// <summary>
        /// 承运商
        /// </summary>
        public Supplier Carrier { get; set; }
        /// <summary>
        /// 派车计划
        /// </summary>
        public WXPCJHItem PCJH { get; set; }
        public static WXDeliveryNotice Parse(DynamicObject obj)
        {
            var item = new WXDeliveryNotice();
            item.Id = Convert.ToInt64(obj["Id"]);
            item.BillNo = Convert.ToString(obj["BillNo"]);
            item.BillDate = Convert.ToDateTime(obj["Date"]);
            item.BillTypeId = Convert.ToString(obj["BillTypeID_Id"]);
            item.BillTypeName = Convert.ToString((obj["BillTypeID"] as DynamicObject)["Name"]);
            item.DocumentStatus = Convert.ToString(obj["DocumentStatus"]);
            item.CloseStatus = Convert.ToString(obj["CloseStatus"]);
            switch (item.DocumentStatus)
            {
                case "Z":
                    item.DocumentStatusName = "暂存";
                    break;
                case "A":
                    item.DocumentStatusName = "创建";
                    break;
                case "B":
                    item.DocumentStatusName = "审核中";
                    break;
                case "C":
                    item.DocumentStatusName = "已审核";
                    break;
                case "D":
                    item.DocumentStatusName = "重新审核";
                    break;
            }
            if (item.CloseStatus == "B")
            {
                item.DocumentStatusName = "已关闭";
            }

            item.Note = Convert.ToString(obj["Note"]);

            var orgObj = obj["DeliveryOrgID"] as DynamicObject;
            item.Organization.Id = Convert.ToInt64(orgObj["Id"]);
            item.Organization.Number = Convert.ToString(orgObj["Number"]);
            item.Organization.Name = Convert.ToString(orgObj["Name"]);

            var cust = obj["CustomerID"] as DynamicObject;
            item.Customer.Id = Convert.ToInt64(cust["Id"]);
            item.Customer.MasterId = Convert.ToInt64(cust["msterID"]);
            item.Customer.Number = Convert.ToString(cust["Number"]);
            item.Customer.Name = Convert.ToString(cust["Name"]);
            var receiver = obj["ReceiverID"] as DynamicObject;
            if (receiver != null)
            {
                item.Receiver.Id = Convert.ToInt64(receiver["Id"]);
                item.Receiver.MasterId = Convert.ToInt64(receiver["msterID"]);
                item.Receiver.Number = Convert.ToString(receiver["Number"]);
                item.Receiver.Name = Convert.ToString(receiver["Name"]);
            }
            var carrier = obj["CarrierID"] as DynamicObject;
            if (carrier != null)
            {
                item.Carrier.Id = Convert.ToInt64(carrier["Id"]);
                item.Carrier.MasterId = Convert.ToInt64(carrier["msterID"]);
                item.Carrier.Number = Convert.ToString(carrier["Number"]);
                item.Carrier.Name = Convert.ToString(carrier["Name"]);
            }

            var entryRows = obj["SAL_DELIVERYNOTICEENTRY"] as DynamicObjectCollection;
            var row = entryRows[0];
            item.EntryId = Convert.ToInt64(row["Id"]);

            var material = row["MaterialId"] as DynamicObject;
            item.Material.Id = Convert.ToInt64(material["Id"]);
            item.Material.MasterId = Convert.ToInt64(material["msterID"]);
            item.Material.Number = Convert.ToString(material["Number"]);
            item.Material.Name = Convert.ToString(material["Name"]);
            item.Material.Specification = Convert.ToString(material["Specification"]);

            var unit = row["UnitId"] as DynamicObject;
            item.Material.StockUnit.Id = Convert.ToInt64(unit["Id"]);
            item.Material.StockUnit.Number = Convert.ToString(unit["Number"]);
            item.Material.StockUnit.Name = Convert.ToString(unit["Name"]);

            item.Material.SaleUnit.Id = item.Material.StockUnit.Id;
            item.Material.SaleUnit.Number = item.Material.StockUnit.Number;
            item.Material.SaleUnit.Name = item.Material.StockUnit.Name;

            item.Material.PurchaseUnit.Id = item.Material.StockUnit.Id;
            item.Material.PurchaseUnit.Number = item.Material.StockUnit.Number;
            item.Material.PurchaseUnit.Name = item.Material.StockUnit.Name;

            item.PlanQty = Convert.ToDecimal(row["Qty"]);
            item.StockQty = Convert.ToDecimal(row["SumOutQty"]);
            item.DeliveryDate = Convert.ToDateTime(row["DeliveryDate"]);

            var subRows = row["FTruckPlanEntity_SD"] as DynamicObjectCollection;
            if (subRows.Count > 0)
            {
                var subRow = subRows[0];
                item.PCJH.Id = Convert.ToInt64(subRow["Id"]);
                item.PCJH.PlateNumber = Convert.ToString(subRow["FCarNum"]);
                item.PCJH.Driver = Convert.ToString(subRow["FDriver"]);
                item.PCJH.IDCardNo = Convert.ToString(subRow["FIDCardNO"]);
                item.PCJH.Mobile = Convert.ToString(subRow["FMobile"]);
                item.PCJH.Qty = Convert.ToDecimal(subRow["FPCQty"]);

                item.PCJH.YunFeiPrice = Convert.ToDecimal(subRow["FYunFeiPrice"]);
                item.PCJH.TruckTypeId = Convert.ToString(subRow["FTruckTypeId_Id"]);
                if (subRow["FTruckTypeId"] != null)
                {
                    item.PCJH.TruckTypeName = Convert.ToString((subRow["FTruckTypeId"] as DynamicObject)["FDataValue"]);
                }

                item.PCJH.ForbidStatus = Convert.ToString(subRow["FRowForbidStatus"]);
                item.PCJH.Note = Convert.ToString(subRow["FPCJHNote_SD"]);
                item.PCJH.ExpiryTime = Convert.ToDateTime(subRow["FPCJHExpiryTime"]).ToString("yyyy-MM-dd HH:mm:ss");
                item.PCJH.RoadTransportCN = Convert.ToString(subRow["FDLYSZH"]);
                item.PCJH.EmploymentQualificationCN = Convert.ToString(subRow["FCYZGZH"]);
                var cheZhou = subRow["FCheZhou"] as DynamicObject;
                if (cheZhou != null)
                {
                    item.PCJH.CheZhou.Id = Convert.ToInt64(cheZhou["Id"]);
                    item.PCJH.CheZhou.Number = Convert.ToString(cheZhou["Number"]);
                    item.PCJH.CheZhou.Name = Convert.ToString(cheZhou["Name"]);
                }
                carrier = subRow["FCarrierID_SD"] as DynamicObject;
                if (carrier != null)
                {
                    item.Carrier.Id = Convert.ToInt64(carrier["Id"]);
                    item.Carrier.MasterId = Convert.ToInt64(carrier["msterID"]);
                    item.Carrier.Number = Convert.ToString(carrier["Number"]);
                    item.Carrier.Name = Convert.ToString(carrier["Name"]);
                }
            }

            return item;
        }
    }
}